package com.CampaignManager.client;

import java.util.Date;

import com.CampaignManager.shared.PlayerDTO;
import com.google.gwt.maps.client.geom.LatLng;

public class MapUtils {
	
public static double getSecondsToTravel(PlayerDTO player) {
		
		LatLng start = GetOrigin(player);
		LatLng end = GetDestination(player);
		double dist = start.distanceFrom(end);
		dist = dist * PlayerDTO.MILES_TO_LATLONG;
		return (dist/PlayerDTO.MILES_PER_DAY)*24*60*60;
	}
	public static double getSecondsTraveledSoFar(PlayerDTO player) {
		
		Date now = new Date();
		return (now.getTime() - player.getMoveStart().getTime())/1000;
	}
		
	public static LatLng GetCurrentLocation(PlayerDTO player) {
		
		double secondsToTravel = getSecondsToTravel(player);
		double secondsTraveledSoFar = getSecondsTraveledSoFar(player);
		
		if (secondsTraveledSoFar >= secondsToTravel)
			return GetDestination(player);
		else {
			double pctWayThere = secondsTraveledSoFar/secondsToTravel;
			double currentLat = player.getOriginLatitude() + pctWayThere * (player.getDestLatitude() - player.getOriginLatitude());
			double currentLng = player.getOriginLongitude() + pctWayThere * (player.getDestLongitude() - player.getOriginLongitude());
			return LatLng.newInstance(currentLat, currentLng);
		} 
	}
	
	public static LatLng GetOrigin(PlayerDTO player) {
		return LatLng.newInstance(player.getOriginLatitude(), player.getOriginLongitude());
	}
	public static LatLng GetDestination(PlayerDTO player) {
		return LatLng.newInstance(player.getDestLatitude(), player.getDestLongitude());
	}

}
